package com.deyuanyun.pic.ctrl.controller.dto;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.beanutils.PropertyUtils;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.poi.annotation.ExcelReadColumn;
import com.deyuanyun.pic.common.validator.annotations.DataValidate;
import com.deyuanyun.pic.ctrl.controller.dto.validata.EQPTRecordValveroomRequestValidata;
import com.deyuanyun.pic.ctrl.controller.dto.validata.EQPTRequestValidata;
import com.deyuanyun.pic.ctrl.controller.dto.validata.EQPTTypeRequestValidata;
import com.deyuanyun.pic.ctrl.domain.CpRecRecode;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.settings.controller.dto.ValveRoomDTO;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.service.pipe.PipeLineService;
import com.deyuanyun.pic.settings.service.pipe.ValveRoomService;

/**
 * 阴保设备整改记录
 * @author ChenKui
 * @date 2016-06-20
 *
 */
public class RepairEQPTRecordExcelVO extends CpRecRecode{

    @ExcelReadColumn(indexChar = "A", columnTitle = "管道名称",columnTitleRowNumber = 2,columnTitleCellNumber = "A")
    @DataValidate(notNull = false,myFunctionMessage = "Excel中的管道与页面选择的不一致")
    private String pipeLineName_;//管道编号
    
    @ExcelReadColumn(indexChar = "B", columnTitle = "所属场站阀室",columnTitleRowNumber = 2,columnTitleCellNumber = "B")
    @DataValidate(notNull = false,myFunction = EQPTRecordValveroomRequestValidata.class,myFunctionMessage = "所属场站阀室不能为空")
    private String pipeValveroomName_;//所属场站阀室
    
    @ExcelReadColumn(indexChar = "C", columnTitle = "设备类型",columnTitleRowNumber = 2,columnTitleCellNumber = "C")
    @DataValidate(notNull = true,myFunction = EQPTTypeRequestValidata.class,myFunctionMessage = "设备类型不是预期的名称")
    private String eqptType_;//设备类型

    @ExcelReadColumn(indexChar = "D", columnTitle = "设备编号",columnTitleRowNumber = 2,columnTitleCellNumber = "D")
    @DataValidate(notNull = true,onlyOne = true,myFunction = EQPTRequestValidata.class,myFunctionMessage = "设备编号或类型有误或者该设备不在该管道范围内")
    private String eqptId_;//设备编号

    @ExcelReadColumn(indexChar = "E", columnTitle = "绝对距离",columnTitleRowNumber = 2,columnTitleCellNumber = "E")
    @DataValidate(notNull = false,myFunctionMessage = "",lengthLimit = "{0,9}",regEx = "^[0-9]+(.[0-9]{1,3})?$",regExMessage="只能是数字")
    private String mileage_;//绝对距离

    @ExcelReadColumn(indexChar = "F", columnTitle = "异常情况",columnTitleRowNumber = 2,columnTitleCellNumber = "F")
    @DataValidate(notNull = true,myFunctionMessage = "")
    private String description;//异常情况 
    

    @ExcelReadColumn(indexChar = "G", columnTitle = "整改时间",columnTitleRowNumber = 2,columnTitleCellNumber = "G")
    @DataValidate(notNull = true,myFunctionMessage = "")
    private String rectificationDate_;//整改时间
    

    @ExcelReadColumn(indexChar = "H", columnTitle = "整改记录",columnTitleRowNumber = 2,columnTitleCellNumber = "H")
    @DataValidate(notNull = true,myFunctionMessage = "")
    private String rectificationDescription;//整改记录
    

    @ExcelReadColumn(indexChar = "I", columnTitle = "整改单位",columnTitleRowNumber = 2,columnTitleCellNumber = "I")
    @DataValidate(notNull = true,myFunctionMessage = "")
    private String rectificationCompany;//整改单位
    

    @ExcelReadColumn(indexChar = "J", columnTitle = "备注",columnTitleRowNumber = 2,columnTitleCellNumber = "J")
    @DataValidate(notNull = false,myFunctionMessage = "")
    private String remarks;//备注


    public void setEqptType_(String eqptType_) {
        EqptTypeEnum eqptType = EqptTypeEnum.getValueByLabel(eqptType_);
        if(eqptType != null){
            setEqptType(eqptType.toString());
        }
        this.eqptType_ = eqptType_;
    }

    public void setEqptId_(String eqptId_) {
        if(StringUtils.isEmpty(eqptType_)){
            this.eqptId_ = eqptId_;
            return ;
        }
        EqptTypeEnum eqptTypeEnum = EqptTypeEnum.getValueByLabel(eqptType_);
        if(eqptTypeEnum == null){
            this.eqptId_ = eqptId_;
            return ;
        }
        EQPTBaseService eQPTBaseService = (EQPTBaseService)SpringAppliction.getBean(EQPTBaseService.class);
        @SuppressWarnings("unchecked")
        Object obj = eQPTBaseService.queryObjectByCustomNumber(eqptTypeEnum.getClazz(), (String)eqptId_);
        if(obj != null){
            String eqptId;
            try {
                eqptId = (String)PropertyUtils.getProperty(obj, "id");
            } catch (Exception e) {
                e.printStackTrace();
                eqptId = null;
            }
            setEqptId(eqptId);
        }
        this.eqptId_ = eqptId_;
        setEqptNumber(eqptId_);
    }

    public void setRectificationDate_(String rectificationDate_) {
        this.rectificationDate_ = rectificationDate_;
        try {
            Date d = DateUtil.allStr2Date(rectificationDate_);
            setRectificationDate(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPipeLineName_(String pipeLineName_) {
        PipeLineService pipeLineService = SpringAppliction.getBean(PipeLineService.class);
        
        PipeLine pipeLine = pipeLineService.getLineByName(pipeLineName_);
        if(pipeLine != null){
            setPipeLineId(pipeLine.getId());
        }
        this.pipeLineName_ = pipeLineName_;
    }

    public void setPipeValveroomName_(String pipeValveroomName_) {
        
        ValveRoomService valveRoomService = SpringAppliction.getBean(ValveRoomService.class);
        ValveRoomDTO valveRoom = valveRoomService.getValueRoomByName(pipeValveroomName_);
        if(valveRoom != null){
            setPipeValveroomId(valveRoom.getId());
        }
        
        this.pipeValveroomName_ = pipeValveroomName_;
    }

    public void setMileage_(String mileage_) {
        this.mileage_ = mileage_;
        if(StringUtils.isEmpty(mileage_)){
            setMileage(null);
            return;
        }
        try {
            setMileage(new BigDecimal(mileage_).movePointRight(3).longValue());    
        } catch (Exception e) {
        }
        
        /*
        String pipeLineId = getPipeLineId();
        ImportService importService = SpringAppliction.getBean(ImportService.class);
        Map<String,Object> map = importService.queryNagm(pipeLineId, mileage_ + "");
        if(map != null && !map.isEmpty()){
            String nagmId = (String)map.get("ID");
            Number absMileage = (Number)map.get("ABSMILEAGE");
            setNagm(nagmId);
            setNagmMileage(absMileage.longValue());
        }
        */
    }
    
    public String getMileage_() {
        return mileage_;
    }

    public String getPipeLineName_() {
        return pipeLineName_;
    }

    public String getPipeValveroomName_() {
        return pipeValveroomName_;
    }

    public String getRectificationDate_() {
        return rectificationDate_;
    }

    public String getEqptType_() {
        return eqptType_;
    }

    public String getEqptId_() {
        return eqptId_;
    }

    
}
